package com.wisdytech.linkdcs.tool.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.PojoConvertUtil;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.tool.dao.ToolMaintainItemBakDao;
import com.wisdytech.linkdcs.tool.dao.ToolMaintainItemDao;
import com.wisdytech.linkdcs.tool.model.ToolMaintainItem;
import com.wisdytech.linkdcs.tool.model.ToolMaintainItemBak;
import com.wisdytech.linkdcs.tool.service.IToolMaintainItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanghailin
 * @since 2018-06-11
 */
@Service
@Transactional
public class ToolMaintainItemServiceImpl extends BaseService<ToolMaintainItemDao, ToolMaintainItem> implements IToolMaintainItemService {

    @Resource
    private ToolMaintainItemDao toolMaintainItemDao;

    @Resource
    private ToolMaintainItemBakDao toolMaintainItemBakDao;

    @Override
    public PageInfo<ToolMaintainItem> getToolMaintainItemByFilter(String toolCode, Filter filter) {
        if (StringUtils.isBlank(toolCode))
            return new PageInfo<>(new ArrayList<>());
        EntityWrapper<ToolMaintainItem> ew = new EntityWrapper<>();
        ew.setSqlSelect("pk_id", "maintain_code", "maintain_name", "maintain_method", "remark")
                .eq("del_flag", CommonConstants.DEL_FLAG)
                .eq("tool_code", toolCode);
        List<ToolMaintainItem> list = this.beforeList(ToolMaintainItemDao.class, filter).selectList(ew);
        return new PageInfo<>(list);
    }

    @Override
    public Boolean saveOrUpdateToolMaintainItem(ToolMaintainItem toolMaintainItem, SysUser operator) {
        Integer operateCount = 0;
        if (operator == null)
            throw new ServiceException("操作人不能为空");
        if (toolMaintainItem == null || StringUtils.isBlank(toolMaintainItem.getMaintainCode()) || StringUtils.isBlank(toolMaintainItem.getToolCode()))
            throw new ServiceException("参数错误");
        //TODO 是否需要校验工装状态

        //校验该工装下保养项编码是否重复
        EntityWrapper<ToolMaintainItem> ew = new EntityWrapper<>();
        ew.eq("tool_code", toolMaintainItem.getToolCode())
                .eq("maintain_code", toolMaintainItem.getMaintainCode())
                .eq("del_flag", CommonConstants.DEL_FLAG);
        if (StringUtils.isNotBlank(toolMaintainItem.getPkId())) {
            ew.ne("pk_id", toolMaintainItem.getPkId());
        }
        Integer count = toolMaintainItemDao.selectCount(ew);
        if (count > 0) {
            throw new ServiceException("保养项重复");
        }
        // 当前记录保存ID
        String pkId = BaseUtil.getUUID();

        String snapshotId = BaseUtil.getUUID();
        //修改
        if (StringUtils.isNotBlank(toolMaintainItem.getPkId())) {
            pkId = toolMaintainItem.getPkId();
            toolMaintainItem.setUpdateDate(new Date());
            toolMaintainItem.setVersion(snapshotId);
            toolMaintainItem.setUpdateUser(operator.getUsername());
            operateCount = toolMaintainItemDao.updateById(toolMaintainItem);
        } else {
            //新增
            toolMaintainItem.setPkId(pkId);
            toolMaintainItem.setCreateDate(new Date());
            toolMaintainItem.setCreateUser(operator.getUsername());
            toolMaintainItem.setDelFlag(CommonConstants.DEL_FLAG);
            toolMaintainItem.setVersion(snapshotId);
            operateCount = toolMaintainItemDao.insert(toolMaintainItem);

        }
        // 查询快照
        ToolMaintainItem snapshot = toolMaintainItemDao.selectById(pkId);
        // 复制快照
        ToolMaintainItemBak toolMaintainItemBak = PojoConvertUtil.convertPojo(snapshot,ToolMaintainItemBak.class);
        toolMaintainItemBak.setDelFlag(CommonConstants.DEL_FLAG);
        toolMaintainItemBak.setPkId(snapshotId);
        // 保存快照
        toolMaintainItemBakDao.insert(toolMaintainItemBak);

        return operateCount > 0;
    }

    @Override
    public Boolean deleteToolMaintainItem(String pkIds, SysUser operator) {
        if (operator == null)
            throw new ServiceException("操作人不能为空");
        String[] deletePkIds = pkIds.split(",");
        if (deletePkIds.length == 0)
            return true;
        List<String> pkIdCollection = Arrays.asList(deletePkIds);
        Integer deleteCount = toolMaintainItemDao.deleteBatchIds(pkIdCollection);
        return deleteCount > 0;
    }

    @Override
    public ToolMaintainItem getToolMaintainItemById(String pkId) {
        if (StringUtils.isBlank(pkId)) {
            return null;
        }
        return toolMaintainItemDao.getToolMaintainItemById(pkId);
    }
}
